package com.ifast.asset.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ifast.asset.service.impl.AssetsServiceImpl;
import com.ifast.asset.util.NewThreadPool;
import com.ifast.asset.domain.AssetAndWeakDO;
import com.ifast.check.entity.WeakPass;
import com.ifast.asset.service.impl.AssetAndWeakServiceImpl;
import com.ifast.asset.util.CommandUtil;
import com.ifast.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("/weakpass")
public class WeakPassController {

    //弱口令破解接口
    @Value("${iniUrl}")
    String iniUrl;
    @Value("${weakpassUrl}")
    String weakpassUrl;
    @Autowired
    AssetsServiceImpl assetsServiceImpl;
    @Autowired
    AssetAndWeakServiceImpl assetAndWeakServiceImpl;

    //检测弱口令接口
    @PostMapping("/check")
    public Result checkWeakPassword(@RequestBody WeakPass weakPass){

        //判断传入参数时否正确
        if (weakPass.getPorts().size()!=weakPass.getProtocols().size()){

            return Result.ok("参数传入错误");
        }else {

            for (int i = 0 ; i < weakPass.getPorts().size() ; i++){

                int port = weakPass.getPorts().get(i);
                String protocol = weakPass.getProtocols().get(i);

                ExecutorService executorService = NewThreadPool.getExecutorService();
                executorService.submit(new CommandUtil(protocol,weakPass.getIp(),port,iniUrl,weakpassUrl,weakPass.getAssetId(),assetsServiceImpl,assetAndWeakServiceImpl));
            }
        }

        return Result.ok();
    }
    //查看弱口令详情接口
    @GetMapping("/find")
    public Result find(Integer assetId){

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("assetId",assetId);
        List<AssetAndWeakDO> lists = assetAndWeakServiceImpl.selectList(wrapper);

        return Result.ok(lists);
    }

}
